import axios from 'axios'
import API from '../api/api.js'

function getHomeBanner(){
	return new Promise((resolve,reject)=>{
		axios.get(`${API.homeBannerApi}?__t={new Date().getTime()}`)
		.then((response)=>{
			//console.log(response.data.data.billboards);
			resolve(response.data.data.billboards);
		})
		.catch((error)=>{
			console.log(error);
		})
	})
}

function getNowPlaying(){
	return new Promise((resolve,reject)=>{
		axios.get(`${API.homeNowPlayingApi}?__t={new Date().getTime()}`)
		.then((response)=>{
			//console.log(response.data.data.films);
			resolve(response.data.data.films);
		})
		.catch((error)=>{
			console.log(error);
		})
	})
}

function getComingSoon(){
	return new Promise((resolve,reject)=>{
		axios.get(`${API.homeComingSoonApi}?__t={new Date().getTime()}&page=1&count=3`)
		.then((response)=>{
			//console.log(response.data.data.films);
			var comingFilm = response.data.data.films.map((item,index)=>{
				var film = {};
				film.id = item.id;
				film.name = item.name;
				var D = new Date(item.premiereAt);
				film.showdate = (D.getMonth() + 1) + '月' + D.getDate() + '日';
				film.imgpath = item.cover.origin;
				return film;
			})
			resolve(comingFilm);
		})
		.catch((error)=>{
			console.log(error);
		})
	})
}

function getFilmNowPlaying(){
	return new Promise((resolve,reject)=>{
		axios.get(`${API.filmNowPlayingApi}?page=1&count=7`)
		.then((response)=>{
			//console.log(response.data.data.films);
			var filmList = response.data.data.films.map((item,index)=>{
				var film = {};
				film.id = item.id;
				film.name = item.name;
				film.imgpath = item.cover.origin;//图片路径
				film.intro = item.intro;//简介
				film.cinemaCount = item.cinemaCount;//上映影院数量
				film.watchCount = item.watchCount;//电影票购买数量
				film.grade = item.grade;//影片评分
				return film;
			})
			resolve(filmList);
		})
		.catch((error)=>{
			console.log(error);
		})
	})
}

function getFilmComingSoon(){
	return new Promise((resolve,reject)=>{
		axios.get(`${API.filmComingSoonApi}?page=1&count=7`)
		.then((response)=>{
			//console.log(response.data.data.films);
			var filmList = response.data.data.films.map((item,index)=>{
				var film = {};
				film.id = item.id;
				film.name = item.name;
				film.imgpath = item.cover.origin;//图片路径
				film.intro = item.intro;//简介
				var D = new Date(item.premiereAt);
				film.showdate = (D.getMonth() + 1) + '月' + D.getDate() + '日';//上映日期
				film.showweek = '星期' + weekFilter(D.getDay());
				return film;
			})
			resolve(filmList);
		})
		.catch((error)=>{
			console.log(error);
		})
	})
}

function getCinema(){
	return new Promise((resolve,reject)=>{
		axios.get(`${API.cinemaApi}?__t={new Date().getTime()}`)
		.then((response)=>{
			console.log(response.data.data.cinemas);
			var arr = [];
			var Arr = response.data.data.cinemas.map((item,index)=>{
				arr.push(item.district.name);
				return arr;
			});
			var arr1 = Array.from(new Set(Arr));
			console.log(arr1);
			resolve(response);
		})
		.catch((error)=>{
			console.log(error);
		})
	})
}

function getShopGoods(){
	return new Promise((resolve,reject)=>{
		axios.get(`${API.shopGoodsApi}`)
		.then((response)=>{
			console.log(response);
			resolve(response);
		})
		.catch((error)=>{
			console.log(error);
		})
	})
}

function getShopSelection(){
	return new Promise((resolve,reject)=>{
		axios.get(`${API.shopSelectionApi}`)
		.then((response)=>{
			console.log(response);
			resolve(response);
		})
		.catch((error)=>{
			console.log(error);
		})
	})
}

function weekFilter(val){//星期过滤
	switch(val){
		case 0 : return '日';break;
		case 1 : return '一';break;
		case 2 : return '二';break;
		case 3 : return '三';break;
		case 4 : return '四';break;
		case 5 : return '五';break;
		case 6 : return '六';break;
		default : return false;break;
	}
}

export default {
	getHomeBanner,
	getNowPlaying,
	getComingSoon,
	getFilmNowPlaying,
	getFilmComingSoon,
	getCinema,
	getShopGoods,
	getShopSelection
}
